package com.jlkf.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.support.logging.Log;
import com.jlkf.pojo.TblApp;
import com.jlkf.pojo.TblCompanyInfo;
import com.jlkf.pojo.TblIntegralSet;
import com.jlkf.service.SystemSetService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;
import com.platform.server.util.UploadUtil;

import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;

@Controller
@RequestMapping("system")
public class SystemSetController {
	@Autowired
	private SystemSetService systemSetService;
	
	private static final Logger logger = LoggerFactory.getLogger(SystemSetController.class);
	
	/**
	 * 关于我们以及客服电话
	 * @param type
	 * @return
	 */
	@RequestMapping("getInfo")
	@ResponseBody
	public ModelAndView getDetail(Integer type){
		TblCompanyInfo info = systemSetService.getInfo();
		ModelAndView andView = new ModelAndView();
    	andView.addObject("info",info);
    	if(type==1){ //关于我们
    		andView.setViewName("jsp/systemSetManager/updateAboutUs");
    	}else if(type==2){ //客服电话
    		andView.setViewName("jsp/systemSetManager/updatePhone");
    	}
    	return andView;
	}
	
	/**
	 * 新增或更新关于我们、客服电话
	 * @param json
	 * @return
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	private ReturnData addOrUpdate(@RequestBody String json){
		try{
			TblCompanyInfo info = StringTools.json2Obj(json, TblCompanyInfo.class);
			int num = 0;
			if(info.getgId()!=null){
				num = systemSetService.updateInfo(info);
			}else{
				num = systemSetService.insertInfo(info);
			}
			if(num>0){
				return new ReturnData(200,"操作成功");
			}else{
				return new ReturnData(500,"操作失败");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
	
	/**
	 * 版本信息
	 * @param type
	 * @return
	 */
	@RequestMapping("getAppInfo")
	@ResponseBody
	public ModelAndView getAppInfo(){
		TblApp info = systemSetService.getAppInfo();
		ModelAndView andView = new ModelAndView();
    	andView.addObject("info",info);
    	andView.setViewName("jsp/systemSetManager/updateApp");
    	return andView;
	}
	
    /**
     * 新增或更新版本信息
     * @param json
     * @return
     */
	@RequestMapping("addOrUpdateApp")
	@ResponseBody
	private ReturnData addOrUpdateApp(@RequestBody String json,HttpServletRequest request){
		try{
			TblApp info = StringTools.json2Obj(json, TblApp.class);
			int num = 0;
			if(info.getgLink()!=null){
				logger.info("apk路径==============="+info.getgLink());
				logger.info("request请求路径==============="+request.getContextPath());
				File file = UploadUtil.downLoadFromUrl(info.getgLink(),  
	                    "app-release.apk",request.getContextPath());
	            ApkFile apkFile = new ApkFile(file);  
	            ApkMeta apkMeta = apkFile.getApkMeta();  
	            logger.info("成功=================="+file);
	            info.setgVersionCode(apkMeta.getVersionCode().toString());  
	            info.setgVersionName(apkMeta.getVersionName());  
			}else{
				return new ReturnData(500,"操作失败");
			}
			if(info.getgId()!=null){
				num = systemSetService.updateAppInfo(info);
			}else{
				num = systemSetService.insertAppInfo(info);
			}
			if(num>0){
				return new ReturnData(200,"操作成功");
			}else{
				return new ReturnData(500,"操作失败");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	} 
}
